//
//  LHHomePageViewController.m
//  Knight_Tour
//
//  Created by Bruce_Lin on 15/5/13.
//  Copyright (c) 2015年 TouchOne. All rights reserved.
//

#import "LHHomePageViewController.h"
#import "LHCountryHomePageCollectionView.h"
#import "LHCountryHomePageTableView.h"
#import "LHSightViewController.h"
#import "MyNavgationView.h"
#import "LHProvinceViewController.h"
#import "LHNetworkHandleWithPost.h"
#import "LHNetworkHandle.h"
#import "LHRotatePicturesModel.h"
#import "LHShowPicturesModel.h"
#import "MBProgressHUD.h"
#import "UIScrollView+MJRefresh.h"

@interface LHHomePageViewController ()<jumpToDetailPageDelegate, UIScrollViewDelegate>
@property (nonatomic, retain)LHCountryHomePageCollectionView *homeCollectionView;
@property (nonatomic, retain)LHCountryHomePageTableView *homeTableView;
@property (nonatomic, retain)UIScrollView *mainScrollView;
@property (nonatomic ,retain)NSMutableArray *rotatePicModelArr;
@property (nonatomic ,retain)NSMutableArray *showPicModelArr;
@property (nonatomic ,retain)NSTimer *rotatimer;
@property (nonatomic ,assign)NSInteger count;
@property (nonatomic ,retain)MBProgressHUD *hud;
@end

@implementation LHHomePageViewController

- (void)dealloc
{
    [_rotatimer release];
    [_rotatePicModelArr release];
    [_homeCollectionView release];
    [_homeTableView release];
    [_mainScrollView release];
    [_showPicModelArr release];
    [_hud release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //初始化容器
    self.rotatePicModelArr = [NSMutableArray array];
    self.showPicModelArr = [NSMutableArray array];
    
    self.count = 1;
    self.navigationController.navigationBar.translucent = NO;
    self.navigationController.navigationBarHidden = YES;

    [self creatNavigation];
    [self getShowPicData];
    [self getRotatePicData];
    
    
    self.hud = [[MBProgressHUD alloc] initWithView:self.navigationController.view];
    [self.navigationController.view addSubview:_hud];
    _hud.dimBackground = YES;
    _hud.minShowTime = 5;
    [_hud show:YES];
    [_hud release];
}

/**
 *  post获取轮播图数据
 */
- (void)getRotatePicData
{
    [LHNetworkHandleWithPost getDataWithURL:@"http://www.roadqu.com/api/mobile/qunawan/tour/seasonalfunplacelist" Body:@"&a_t=1431305603000&token=554dbf3478d18&sign=9f85335cced8400f8cd72e98b605f37f56697962" completion:^(id result) {

        for (NSDictionary *tempdic in [result objectForKey:@"data"]) {
            
            LHRotatePicturesModel * model = [[LHRotatePicturesModel alloc]initWithDic:tempdic];
            [self.rotatePicModelArr addObject:model];
            [model release];
        }
        
        [self createView];
    }];
}

/**
 *  获取展示图片数据
 */
- (void)getShowPicData
{
//    [LHNetworkHandleWithPost getDataWithURL:@"http://www.roadqu.com/api/mobile/qunawan/tour/featuredthemeslist" Body:@"&a_t=1431305604000&token=554dbf3478d18&sign=2cf70dfdce49b816018e4aa0962feb02d519e61f" completion:^(id result) {
//        
//        for (NSDictionary *tempdic in [result objectForKey:@"data"]) {
//            
//            LHShowPicturesModel *model = [[LHShowPicturesModel alloc]initWithDic:tempdic];
//            [self.showPicModelArr addObject:model];
//            [model release];
//            [_homeTableView.homePageTableView reloadData];
//        }
//        [self creattableView];
//    }];
    [LHNetworkHandle getDataWithURL:@"http://www.roadqu.com/api/mobile/qunawan/tour/featuredthemeslist&a_t=1431305604000&token=554dbf3478d18&sign=2cf70dfdce49b816018e4aa0962feb02d519e61f" completion:^(id result) {
        
        for (NSDictionary *tempdic in [result objectForKey:@"data"]) {
            
            LHShowPicturesModel *model = [[LHShowPicturesModel alloc]initWithDic:tempdic];
            [self.showPicModelArr addObject:model];
            [model release];
//            [_homeTableView.homePageTableView reloadData];
        }
        NSLog(@"%@",self.showPicModelArr);
        if (self.showPicModelArr.count == 0) {
            [self getShowPicData];
        }
        
        [self creattableView];
    }];
    
}

/**
 *  //创建导航栏
 */
- (void)creatNavigation
{
    MyNavgationView *navgation = [[MyNavgationView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 64)];
    
    [self.view addSubview:navgation];
    [navgation release];
    
    navgation.rightTarget = self;
    navgation.rightAction = @selector(pushToProvince);
    [navgation creatRightButtonWithLabel:@"全国景点"];
}

/**
 *  创建主页视图
 */
- (void)createView
{
    self.mainScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 64, self.view.frame.size.width, self.view.frame.size.height)];
    _mainScrollView.contentSize = CGSizeMake(_mainScrollView.frame.size.width, (LHheight / 4 + 5) * (11 - 2)+ LHheight / 3  + 20);
    _mainScrollView.delegate = self;
    [self.view addSubview:_mainScrollView];
    [_mainScrollView release];
    
    self.mainScrollView.contentInset = UIEdgeInsetsMake(0, 0, 64, 0);
    
    //创建轮播图
    self.homeCollectionView = [[LHCountryHomePageCollectionView alloc]initWithFrame:CGRectMake(0, 0, LHWidth, LHheight / 3) picModelArr:self.rotatePicModelArr];
    
    [_mainScrollView addSubview:_homeCollectionView];
    [_homeCollectionView release];
    
    //添加定时器
    self.rotatimer = [NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(rotate) userInfo:nil repeats:YES];
}

- (void)creattableView
{
    _mainScrollView.contentSize = CGSizeMake(_mainScrollView.frame.size.width, (LHheight / 4 + 5) *  (11 - 2) + LHheight / 3 + 20);
    //创建tableview
    self.homeTableView = [[LHCountryHomePageTableView alloc]initWithFrame:CGRectMake(0, _homeCollectionView.frame.size.height, self.view.frame.size.width, LHheight * 3 - self.view.frame.size.height / 3)];
    
    self.automaticallyAdjustsScrollViewInsets = NO;
   
    self.homeTableView.showPicArr = self.showPicModelArr;
    self.homeTableView.homePageTableView.scrollEnabled = NO;
   
    //签订跳转协议
    _homeTableView.delegate = self;

    [_mainScrollView addSubview:_homeTableView];
    [_homeTableView release];
    [_hud removeFromSuperview];
}


- (void)rotate
{
    [self.homeCollectionView.titlePicScrollView setContentOffset:CGPointMake(LHWidth * self.count, 0)];
    if (self.count == 5) {
        
        [self.homeCollectionView.titlePicScrollView setContentOffset:CGPointMake(LHWidth, 0)];
        self.count = 0;
    }
    self.count += 1;
}

/**
 *  实现跳转协议方法
 */
- (void)jumpToDetailWithIndexPath:(NSIndexPath *)indexPath
{
    LHSightViewController *sightVC = [[LHSightViewController alloc]init];
    sightVC.hidesBottomBarWhenPushed = YES;
    sightVC.clickIndexPath = indexPath;
    [self.navigationController pushViewController:sightVC animated:YES];
    [sightVC release];
}

/**
 *  导航右侧按钮响应方法
 */
- (void)pushToProvince
{
    LHProvinceViewController *vc = [[LHProvinceViewController alloc]init];
    vc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:vc animated:YES];
    [vc release];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
